package com.sy.server.service.impl;

import com.sy.server.dto.CostWithoutDetail;
import com.sy.server.mapper.CostWithoutDetailMapper;
import com.sy.server.service.IExcelService;
import com.sy.server.util.DateUtil;
import com.sy.server.util.ExcelUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ExcelService implements IExcelService {
    @Autowired
    private CostWithoutDetailMapper mapper;

    @Autowired
    private IpService ipService;

    private Logger logger= LoggerFactory.getLogger(ExcelService.class);

    @Value("${file.excel.name}")
    private String fileName;
    @Value("${file.excel.path}")
    private String filePath;

    public String writeExcel(Integer year,Integer month) {
        Integer userId=ipService.getUserIdByCurrentIp();
        String userName=ipService.getUserNameByCurrentIp();
        List<CostWithoutDetail> list=mapper.selectByBeginEndDay(userId, DateUtil.processingParams(year,month)[0],DateUtil.processingParams(year,month)[1]);
        String newFileName=fileName+"-"+userName+"-"+System.currentTimeMillis()+".xls";
        String newFilePath=filePath+newFileName;
        logger.info("准备写入xcEel，文件路径为：{}",newFilePath);
        ExcelUtil.write(newFilePath,list);
        logger.info("文件已写入，文件名：{}",newFileName);
        return newFileName;
    }
}
